package com.sl.au.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.sl.au.service.OperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Attachment;
import com.sl.au.entity.Module;
import com.sl.au.entity.News;
import com.sl.au.entity.ReplacementDevice;
import com.sl.au.entity.User;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.NewsService;
import com.sl.au.service.OptionService;

@RestController
@RequestMapping(value="/ui/news")
public class NewsController extends BaseController{

	static String MODULE_NAME="cir";//公司信息发布

	
    @Autowired
    NewsService NewsService;
    
    @Autowired
    AttachmentService attachmentService;
    

    @Autowired
	OptionService optionService;

    @Autowired
    OperateLogService operateLogService;
  
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(){
    	ModelAndView modelAndView=createLayoutView("news/newsindex");
    	Account a = this.getMyAccount();
        String superior=a.getUser().getSaleArea().getSuperior();
        if(superior.equals("销售处")){
        	modelAndView.addObject("types", optionService.getRepository().findByGroup("公司信息类型-销售处"));
        }else if(superior.equals("成套处")){
            modelAndView.addObject("types", optionService.getRepository().findByGroup("公司信息类型-成套处"));
        }else if(superior.equals("外贸处")){
            modelAndView.addObject("types", optionService.getRepository().findByGroup("公司信息类型-外贸处"));
        }else if (superior.equals("售后服务处")){
            modelAndView.addObject("types", optionService.getRepository().findByGroup("公司信息类型-售后服务处"));
        }
    	Module module=this.getPageModule(NewsController.MODULE_NAME);    
        
        
        modelAndView.addObject("module",module);
        return modelAndView;

    }
    /**
     * 查看次数
     * @param request
     * @return
     */
    @RequestMapping(value = "witch", method = RequestMethod.GET)
    public Map<String, Object> witchNumber(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	String id= request.getParameter("id");
    	News old=NewsService.getRepository().findOne(id);
    	int number;
    	if(old.getnWatchNumber() == null){
    		number = 0;
    	}else{
    		number = old.getnWatchNumber();
    	}
    	int watchNumber = number+1;
    	old.setnWatchNumber(watchNumber);
    	NewsService.getRepository().save(old);
    	map.put("code", 0);
        return map;
    }
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
    public Map<String, Object> getPage(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> condition = new HashMap<String, String>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key=request.getParameter("key");
        if (key==null){
        	key = "";
        }
        Account a = this.getMyAccount();
		Module module = this.getPageModule("cir");
        String dataFilter=module.getDataFilter();
		User user = a.getUser();
		String userId=user.getId();
		String superior=user.getSaleArea().getSuperior();
		String areaId=user.getSaleArea().getId();
		condition.put("key", key);
		condition.put("dataFilter", dataFilter);
		condition.put("superior", superior);
		condition.put("userId", userId);
		condition.put("areaId", areaId);
        List<News> news=null;
            news = NewsService.getNews(start, length, condition, null, null);
        Long count = NewsService.getNewsCount(condition);
        
        List<News> news1 = new ArrayList<News>();
        for(News n:news){
        	String parentId=n.getId();
			List<Attachment> a1=attachmentService.getRepository().findByParentId(parentId);
			int num=a1.size();
			n.setAttachNumber(num);
			news1.add(n);
        }
        map.put("data", news1);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
	
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Map<String, Object> saveNews(HttpServletRequest request, @RequestBody News news) {
        Map<String, Object> map = new HashMap<String, Object>();
        Account a = this.getMyAccount();
        User user = a.getUser();
        Date nowDate=new Date();
        news.setCreateTime(nowDate);
        news.setUser(user);
        News old=NewsService.getRepository().findOne(news.getId());
        if(old!=null){
            String content  = "编辑公司信息【" + news.getTitle() + "】";
            operateLogService.log(user, "编辑公司信息", content);
        }else{
            String content  = "新增公司信息【" + news.getTitle() + "】";
            operateLogService.log(user, "新增公司信息", content);
        }

        NewsService.getRepository().save(news);
        String newsId = news.getId();
        Date createTime = news.getCreateTime();
        map.put("newsId", newsId);
        map.put("createTime", createTime);
        map.put("news", news);
        return map;
    }
    ///删除
    @RequestMapping(value = "del", method = RequestMethod.GET)
    public Map<String, Object> delAfterSaleReturnVisit(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        String ids=request.getParameter("ids");
        String id[]=ids.split(",");
        List<News> news=new ArrayList<News>();
        for(int i=0;i<id.length;i++){
            String newsId = id[i];
            News News=new News();
            News.setId(newsId);
            news.add(News);
        }
        /////插入删除日志
        Account a = this.getMyAccount();
        User user = a.getUser();
        for(News d:news){
            News old=NewsService.getRepository().findOne(d.getId());
            String content  = "删除公司信息【" + old.getTitle() + "】";
            operateLogService.log(user, "删除公司信息", content);
        }

        NewsService.getRepository().deleteInBatch(news);
        map.put("code", 0);
        return map;
    }
    ///公司信息发布
    @RequestMapping(value = "publish", method = RequestMethod.GET)
    public Map<String, Object> publish(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        String ids=request.getParameter("ids");
        String id[]=ids.split(",");
        List<News> news=new ArrayList<News>();
        for(int i=0;i<id.length;i++){
            String newsId = id[i];
            News News=NewsService.getRepository().findOne(newsId);
            if(News != null){
            	News.setIsPublished(1);
            }
            news.add(News);
        }
        Account a = this.getMyAccount();
        User user = a.getUser();
        for(News b:news){
            News old=NewsService.getRepository().findOne(b.getId());
            String content  = "发布公司信息【" + old.getTitle() + "】";
            operateLogService.log(user, "发布公司信息", content);
        }
        NewsService.getRepository().save(news);
        map.put("code", 0);
        return map;
    }
}
